home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tar.gnu / sprite / RCS / update.c,v < prev   
Encoding:
Text File  |  1992-03-29  |  13.5 KB  |  597 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     92.03.28.17.32.17;  author kupfer;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     92.03.15.18.53.35;  author kupfer;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @Functions for updating a tar archive.
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @Restructure to remove some of the ALLOW_LONG_NAMES ifdefs.  Fix to
  28. allow both a long file name and a long link name.  Check for Posix
  29. archives.
  30. @
  31. text
  32. @/* Update a tar archive.
  33.    Copyright (C) 1988 Free Software Foundation
  34.  
  35. This file is part of GNU Tar.
  36.  
  37. GNU Tar is free software; you can redistribute it and/or modify
  38. it under the terms of the GNU General Public License as published by
  39. the Free Software Foundation; either version 1, or (at your option)
  40. any later version.
  41.  
  42. GNU Tar is distributed in the hope that it will be useful,
  43. but WITHOUT ANY WARRANTY; without even the implied warranty of
  44. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  45. GNU General Public License for more details.
  46.  
  47. You should have received a copy of the GNU General Public License
  48. along with GNU Tar; see the file COPYING.  If not, write to
  49. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  50.  
  51. /* JF implement the 'r' 'u' and 'A' options for tar. */
  52. /* The 'A' option is my own invention:  It means that the file-names are
  53.    tar files, and they should simply be appended to the end of the archive.
  54.    No attempt is made to block the reads from the args; if they're on raw
  55.    tape or something like that, it'll probably lose. . . */
  56.  
  57. #include <sys/types.h>
  58. #include <sys/stat.h>
  59. #include <stdio.h>
  60. #include <errno.h>
  61.  
  62. /* JF these includes are copied from create.c  I'm not sure if they're right
  63.    or not.  */
  64. #ifndef V7
  65. #include <fcntl.h>
  66. #endif
  67.  
  68. #ifndef    MSDOS
  69. #include <pwd.h>
  70. #include <grp.h>
  71. #endif
  72.  
  73. #ifdef USG
  74. #include <sys/sysmacros.h>    /* major() and minor() defined here */
  75. #endif
  76.  
  77. /*
  78.  * V7 doesn't have a #define for this.
  79.  */
  80. #ifndef O_RDONLY
  81. #define    O_RDONLY    0
  82. #endif
  83.  
  84. /*
  85.  * Most people don't have a #define for this.
  86.  */
  87. #ifndef    O_BINARY
  88. #define    O_BINARY    0
  89. #endif
  90.  
  91. #define STDIN 0
  92. #define STDOUT 1
  93.  
  94. #include "tar.h"
  95. #include "port.h"
  96. #include "rmt.h"
  97.  
  98. int time_to_start_writing = 0;    /* We've hit the end of the old stuff,
  99.                    and its time to start writing new stuff
  100.                    to the tape.  This involves seeking
  101.                    back one block and re-writing the current
  102.                    block (which has been changed). */
  103.  
  104. char *output_start;        /* Pointer to where we started to write in
  105.                    the first block we write out.  This is used
  106.                    if we can't backspace the output and have
  107.                    to null out the first part of the block */
  108.  
  109. extern void skip_file();
  110. extern void skip_extended_headers();
  111.  
  112. extern union record *head;
  113. extern struct stat hstat;
  114.  
  115. struct name *name_scan();
  116. char    *name_from_list();
  117.  
  118. /* Implement the 'r' (add files to end of archive), and 'u' (add files to
  119.    end of archive if they arent there, or are more up to date than the
  120.    version in the archive.) commands.*/
  121. void
  122. update_archive()
  123. {
  124.     int    found_end = 0;
  125.     int    status = 3;
  126.     int    prev_status;
  127.     char    *p;
  128.     struct name *name;
  129.     extern void dump_file();
  130.  
  131.     name_gather();
  132.     if(cmd_mode==CMD_UPDATE)
  133.         name_expand();
  134.     open_archive(2);    /* Open for updating */
  135.  
  136.     do {
  137.         prev_status=status;
  138.         status=read_header();
  139.         switch(status) {
  140.         case EOF:
  141.             found_end=1;
  142.             break;
  143.  
  144.         case 0:        /* A bad record */
  145.             userec(head);
  146.             switch(prev_status) {
  147.             case 3:
  148.                 msg("This doesn't look like a tar archive.\n");
  149.                 /* FALL THROUGH */
  150.             case 2:
  151.             case 1:
  152.                 msg("Skipping to next header\n");
  153.             case 0:
  154.                 break;
  155.             }
  156.             break;
  157.  
  158.             /* A good record */
  159.         case 1:
  160.  /* printf("File %s\n",head->header.name); */
  161.             head->header.name[NAMSIZ-1]='\0';
  162.             saverec(&head);    /* hold onto the header */
  163.             userec(head); /* and skip past it in the archive */
  164.             get_names(head);
  165.             if(cmd_mode==CMD_UPDATE &&
  166.                (name=name_scan(current_filename))) {
  167.                 /* struct stat hstat; */
  168.                 struct stat nstat;
  169.                 int head_standard;
  170.  
  171.                 decode_header(head,&hstat,&head_standard,0);
  172.                 if(stat(current_filename,&nstat)<0) {
  173.                     msg_perror("can't stat %s:",
  174.                            current_filename);
  175.                 } else {
  176.                     if(hstat.st_mtime>=nstat.st_mtime)
  177.                         name->found++;
  178.                 }
  179.             }
  180.             if (gnu_extended_header(head))
  181.                 skip_extended_headers();
  182.             skip_file((long)hstat.st_size);
  183.             saverec((union record **)0);
  184.             break;
  185.  
  186.         case 2:
  187.             ar_record=head;
  188.             found_end = 1;
  189.             break;
  190.         }
  191.     } while(!found_end);
  192.  
  193.     time_to_start_writing = 1;
  194.     output_start=ar_record->charptr;
  195.  
  196.     while(p=name_from_list()) {
  197.         if(f_confirm && !confirm("add", p))
  198.             continue;
  199.         if(cmd_mode==CMD_CAT)
  200.             append_file(p);
  201.         else
  202.             dump_file(p,-1);
  203.     }
  204.  
  205.     write_eot();
  206.     close_archive();
  207.     names_notfound();
  208. }
  209.  
  210. /* Catenate file p to the archive without creating a header for it.  It had
  211.    better be a tar file or the archive is screwed */
  212.  
  213. append_file(p)
  214. char *p;
  215. {
  216.     int    fd;
  217.     struct stat statbuf;
  218.     long    bytes_left;
  219.     union record *start;
  220.     long    bufsiz,count;
  221.  
  222.     if(0 != stat(p,&statbuf) || (fd=open(p,O_RDONLY|O_BINARY))<0) {
  223.         msg_perror("can't open file %s",p);
  224.         errors++;
  225.         return;
  226.     }
  227.  
  228.     bytes_left = statbuf.st_size;
  229.  
  230.     while(bytes_left>0) {
  231.         start=findrec();
  232.         bufsiz=endofrecs()->charptr - start->charptr;
  233.         if(bytes_left < bufsiz) {
  234.             bufsiz = bytes_left;
  235.             count = bufsiz % RECORDSIZE;
  236.             if(count)
  237.                 bzero(start->charptr + bytes_left,(int)(RECORDSIZE-count));
  238.         }
  239.         count=read(fd,start->charptr,bufsiz);
  240.         if(count<0) {
  241.             msg_perror("read error at byte %ld reading %d bytes in file %s",statbuf.st_size-bytes_left,bufsiz,p);
  242.             exit(EX_ARGSBAD);        /* FOO */
  243.         }
  244.         bytes_left-=count;
  245.         userec(start+(count-1)/RECORDSIZE);
  246.         if(count!=bufsiz) {
  247.             msg("%s: file shrunk by %d bytes, yark!\n",p,bytes_left);
  248.             abort();
  249.         }
  250.     }
  251.     (void)close(fd);
  252. }
  253.  
  254. #ifdef DONTDEF
  255. bprint(fp,buf,num)
  256. FILE *fp;
  257. char *buf;
  258. {
  259.     int    c;
  260.  
  261.     if(num==0 || num==-1)
  262.         return;
  263.     fputs(" '",fp);
  264.     while(num--) {
  265.         c= *buf++;
  266.         if(c=='\\') fputs("\\\\",fp);
  267.         else if(c>=' ' && c<='~')
  268.             putc(c,fp);
  269.         else switch(c) {
  270.         case '\n':
  271.             fputs("\\n",fp);
  272.             break;
  273.         case '\r':
  274.             fputs("\\r",fp);
  275.             break;
  276.         case '\b':
  277.             fputs("\\b",fp);
  278.             break;
  279.         case '\0':
  280.             /* fputs("\\-",fp); */
  281.             break;
  282.         default:
  283.             fprintf(fp,"\\%03o",c);
  284.             break;
  285.         }
  286.     }
  287.     fputs("'\n",fp);
  288. }
  289. #endif
  290.  
  291. int number_of_blocks_read = 0;
  292.  
  293. int number_of_new_records = 0;
  294. int number_of_records_needed = 0;
  295.  
  296. union record *new_block = 0;
  297. union record *save_block = 0;
  298.  
  299. void
  300. junk_archive()
  301. {
  302.     int    found_stuff = 0;
  303.     int    status = 3;
  304.     int    prev_status;
  305.     struct name *name;
  306.  
  307.     /* int dummy_head; */
  308.     int number_of_records_to_skip = 0;
  309.     int number_of_records_to_keep = 0;
  310.     int number_of_kept_records_in_block;
  311.     int sub_status;
  312.     extern int write_archive_to_stdout;
  313.  
  314. /* fprintf(stderr,"Junk files\n"); */
  315.     name_gather();
  316.     open_archive(2);
  317.  
  318.     while(!found_stuff) {
  319.         prev_status=status;
  320.         status=read_header();
  321.         switch(status) {
  322.         case EOF:
  323.             found_stuff = 1;
  324.             break;
  325.  
  326.         case 0:
  327.             userec(head);
  328.             switch(prev_status) {
  329.             case 3:
  330.                 msg("This doesn't look like a tar archive.\n");
  331.                 /* FALL THROUGH */
  332.             case 2:
  333.             case 1:
  334.                 msg("Skipping to next header\n");
  335.                 /* FALL THROUGH */
  336.             case 0:
  337.                 break;
  338.             }
  339.             break;
  340.  
  341.         case 1:
  342.             head->header.name[NAMSIZ-1] = '\0';
  343.  /* fprintf(stderr,"file %s\n",head->header.name); */
  344.             if((name=name_scan(head->header.name))==(struct name *)0) {
  345.                 userec(head);
  346.  /* fprintf(stderr,"Skip %ld\n",(long)(hstat.st_size)); */
  347.                  if (gnu_extended_header(head))
  348.                     skip_extended_headers();
  349.                 skip_file((long)(hstat.st_size));
  350.                 break;
  351.             }
  352.             name->found = 1;
  353.             found_stuff = 2;
  354.             break;
  355.  
  356.         case 2:
  357.             found_stuff = 1;
  358.             break;
  359.         }
  360.     }
  361.  /* fprintf(stderr,"Out of first loop\n"); */
  362.  
  363.     if(found_stuff!=2) {
  364.         write_eot();
  365.         close_archive();
  366.         names_notfound();
  367.         return;
  368.     }
  369.  
  370.     if(write_archive_to_stdout)
  371.         write_archive_to_stdout = 0;
  372.     new_block = (union record *)malloc(blocksize);
  373.     if(new_block==0) {
  374.         fprintf(stderr,"Can't allocate secondary block of %d bytes\n",blocksize);
  375.         exit(EX_SYSTEM);
  376.     }
  377.  
  378.         /* Save away records before this one in this block */
  379.     number_of_new_records=ar_record-ar_block;
  380.     number_of_records_needed = blocking - number_of_new_records;
  381.     if(number_of_new_records)
  382.         bcopy((void *)ar_block,(void *)new_block,(number_of_new_records)*RECORDSIZE);
  383.  
  384.  /* fprintf(stderr,"Saved %d recs, need %d more\n",number_of_new_records,number_of_records_needed); */
  385.     userec(head);
  386.     if (gnu_extended_header(head))
  387.         skip_extended_headers();
  388.     skip_file((long)(hstat.st_size));
  389.     found_stuff=0;
  390.     /* goto flush_file; */
  391.  
  392.     for(;;) {
  393.             /* Fill in a block */
  394.     /* another_file: */
  395.         if(ar_record==ar_last) {
  396.  /* fprintf(stderr,"New block\n"); */
  397.             flush_archive();
  398.             number_of_blocks_read++;
  399.         }
  400.         sub_status = read_header();
  401.  /* fprintf(stderr,"Header type %d\n",sub_status); */
  402.  
  403.         if(sub_status==2 && f_ignorez) {
  404.             userec(head);
  405.             continue;
  406.         }
  407.         if(sub_status==EOF || sub_status==2) {
  408.             found_stuff = 1;
  409.             bzero(new_block[number_of_new_records].charptr,RECORDSIZE*number_of_records_needed);
  410.             number_of_new_records+=number_of_records_needed;
  411.             number_of_records_needed = 0;
  412.             write_block(0);
  413.             break;
  414.         }
  415.  
  416.         if(sub_status==0) {
  417.             fprintf(stderr,"Deleting non-header from archive.\n");
  418.             userec(head);
  419.             continue;
  420.         }
  421.  
  422.         /* Found another header.  Yipee! */
  423.         head->header.name[NAMSIZ-1] = '\0';
  424.  /* fprintf(stderr,"File %s ",head->header.name); */
  425.         if(name=name_scan(head->header.name)) {
  426.             name->found = 1;
  427.  /* fprintf(stderr,"Flush it\n"); */
  428.         /* flush_file: */
  429.             /* decode_header(head,&hstat,&dummy_head,0); */
  430.             userec(head);
  431.             number_of_records_to_skip=(hstat.st_size+RECORDSIZE-1)/RECORDSIZE;
  432.  /* fprintf(stderr,"Flushing %d recs from %s\n",number_of_records_to_skip,head->header.name); */
  433.  
  434.             while(ar_last-ar_record<=number_of_records_to_skip) {
  435.  
  436.  /* fprintf(stderr,"Block: %d <= %d  ",ar_last-ar_record,number_of_records_to_skip); */
  437.                 number_of_records_to_skip -= (ar_last - ar_record);
  438.                 flush_archive();
  439.                 number_of_blocks_read++;
  440.  /* fprintf(stderr,"Block %d left\n",number_of_records_to_skip); */
  441.             }
  442.             ar_record+=number_of_records_to_skip;
  443.  /* fprintf(stderr,"Final %d\n",number_of_records_to_skip); */
  444.             number_of_records_to_skip = 0;
  445.             continue;
  446.         }
  447.  
  448.     /* copy_header: */
  449.         new_block[number_of_new_records]= *head;
  450.         number_of_new_records++;
  451.         number_of_records_needed--;
  452.         number_of_records_to_keep=(hstat.st_size+RECORDSIZE-1)/RECORDSIZE;
  453.         userec(head);
  454.         if(number_of_records_needed==0)
  455.             write_block(1);
  456.     /* copy_data: */
  457.         number_of_kept_records_in_block = ar_last - ar_record;
  458.         if(number_of_kept_records_in_block > number_of_records_to_keep)
  459.             number_of_kept_records_in_block = number_of_records_to_keep;
  460.  
  461.  /* fprintf(stderr,"Need %d kept_in %d keep %d\n",blocking,number_of_kept_records_in_block,number_of_records_to_keep); */
  462.  
  463.         while(number_of_records_to_keep) {
  464.             int n;
  465.  
  466.             if(ar_record==ar_last) {
  467.  /* fprintf(stderr,"Flush. . .\n"); */
  468.                 fl_read();
  469.                 number_of_blocks_read++;
  470.                 ar_record=ar_block;
  471.                 number_of_kept_records_in_block = blocking;
  472.                 if(number_of_kept_records_in_block > number_of_records_to_keep)
  473.                     number_of_kept_records_in_block = number_of_records_to_keep;
  474.             }
  475.             n = number_of_kept_records_in_block;
  476.             if(n>number_of_records_needed)
  477.                 n = number_of_records_needed;
  478.  
  479.  /* fprintf(stderr,"Copying %d\n",n); */
  480.             bcopy((void *)ar_record, (void *)(new_block+number_of_new_records), n*RECORDSIZE);
  481.             number_of_new_records           += n;
  482.             number_of_records_needed        -= n;
  483.             ar_record                       += n;
  484.             number_of_records_to_keep       -= n;
  485.             number_of_kept_records_in_block -= n;
  486.  /* fprintf(stderr,"Now new %d  need %d  keep %d  keep_in %d rec %d/%d\n", 
  487.  number_of_new_records,number_of_records_needed,number_of_records_to_keep,
  488.  number_of_kept_records_in_block,ar_record-ar_block,ar_last-ar_block); */
  489.  
  490.             if(number_of_records_needed == 0) {
  491.                 write_block(1);
  492.             }
  493.         }
  494.     }
  495.  
  496.     write_eot();
  497.     close_archive();
  498.     names_notfound();
  499. }
  500.  
  501. write_block(f)
  502. {
  503.  /* fprintf(stderr,"Write block\n"); */
  504.     /* We've filled out a block.  Write it out. */
  505.  
  506.     /* Backspace back to where we started. . . */
  507.     if(archive!=STDIN)
  508.         (void)move_arch(-(number_of_blocks_read+1));
  509.  
  510.     save_block = ar_block;
  511.     ar_block = new_block;
  512.  
  513.     if(archive==STDIN)
  514.         archive=STDOUT;
  515.     fl_write();
  516.  
  517.     if(archive==STDOUT)
  518.         archive=STDIN;
  519.     ar_block = save_block;
  520.  
  521.     if(f) {
  522.         /* Move the tape head back to where we were */
  523.         if(archive!=STDIN)
  524.             (void)move_arch(number_of_blocks_read);
  525.         number_of_blocks_read--;
  526.     }
  527.  
  528.     number_of_records_needed = blocking;
  529.     number_of_new_records = 0;
  530. }
  531.  
  532. /* Move archive descriptor by n blocks worth.  If n is positive we move
  533.    forward, else we move negative.   If its a tape, MTIOCTOP had better
  534.    work.  If its something else, we try to seek on it.  If we can't
  535.    seek, we lose! */
  536. move_arch(n)
  537. {
  538.     long cur;
  539.     extern int errno;
  540.  
  541. #ifdef MTIOCTOP
  542.     struct mtop t;
  543.     int er;
  544.  
  545.     if(n>0) {
  546.         t.mt_op = MTFSR;
  547.         t.mt_count = n;
  548.     } else {
  549.         t.mt_op = MTBSR;
  550.         t.mt_count = -n;
  551.     }
  552.     if((er=rmtioctl(archive,MTIOCTOP,&t))>=0)
  553.         return 1;
  554.     if(errno==EIO && (er=rmtioctl(archive,MTIOCTOP,&t))>=0)
  555.         return 1;
  556. #endif
  557.  
  558.     cur=rmtlseek(archive,0L,1);
  559.     cur+=blocksize*n;
  560.  
  561.  /* fprintf(stderr,"Fore to %x\n",cur); */
  562.     if(rmtlseek(archive,cur,0)!=cur) {
  563.         /* Lseek failed.  Try a different method */
  564.         fprintf(stderr,"tar: Couldn't re-position archive file.\n");
  565.         exit(EX_BADARCH);
  566.     }
  567.     return 3;
  568. }
  569.  
  570. @
  571.  
  572.  
  573. 1.1
  574. log
  575. @Initial revision
  576. @
  577. text
  578. @d131 5
  579. a135 1
  580.             if(cmd_mode==CMD_UPDATE && (name=name_scan(head->header.name))) {
  581. d141 3
  582. a143 2
  583.                 if(stat(head->header.name,&nstat)<0) {
  584.                     msg_perror("can't stat %s:",head->header.name);
  585. d149 1
  586. a149 2
  587.             userec(head);
  588.             if (head->header.isextended)
  589. d152 1
  590. d316 1
  591. a316 1
  592.                  if (head->header.isextended)
  593. d355 1
  594. a355 1
  595.     if (head->header.isextended)
  596. @
  597.